package com.supinfo.sun.sungamers.dao.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import com.supinfo.sun.sungamers.dao.CityDAO;

/**
 * @author Liu, Bing(Locke)
 * @since 2011-04-05
 * @version 0.3
 */
public class CitiesFile implements CityDAO {

	public File getCitiesFile() {
		File file = new File("./cities.sg");
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				System.out.println("failed to create a file named cities.sg");
			}
		}
		return file;
	}

	public void insertSomeCities() {
		// binaryInsert();
		charInsert();
	}

	private void binaryInsert() {
		try {
			FileOutputStream fos = new FileOutputStream(getCitiesFile());

			fos.write("Yangzhou".getBytes());
			fos.write("\n".getBytes());
			fos.write("Zhenjiang".getBytes());
			fos.write("\n".getBytes());
			fos.write("Shanghai".getBytes());
			System.out.println("end of output");
			fos.flush();
			fos.close();
		} catch (FileNotFoundException e) {
			System.out.println("File not found");
		} catch (IOException e) {
			System.out.println("failed to output");
		}
	}

	private void charInsert() {
		try {
			PrintWriter pw = new PrintWriter(getCitiesFile());

			pw.write("Tours");
			pw.write("\n");
			pw.write("Paris");
			pw.write("\n");
			pw.write("Marseille");
			System.out.println("end of output");
			pw.flush();
			pw.close();
		} catch (FileNotFoundException e) {
			System.out.println("File not found");
		}
	}

	public List<String> getAllCities() {
		List<String> citiesList = new ArrayList<String>();

		try {
			FileReader fr = new FileReader(getCitiesFile());
			BufferedReader br = new BufferedReader(fr);

			String city;
			while ((city = br.readLine()) != null) {
				citiesList.add(city);
			}
		} catch (FileNotFoundException e) {
			System.out.println("File not found");
		} catch (IOException e) {
			System.out.println("failed to output");
		}

		return citiesList;
	}

	public List<String> getCities() {
		// TODO Auto-generated method stub
		return null;
	}

	public void appendCity(String city) {
		try {
			FileWriter fw = new FileWriter(getCitiesFile(), true);
			PrintWriter pw = new PrintWriter(fw);
			pw.append(city);
			pw.append("\n");
			pw.flush();
			pw.close();
		} catch (FileNotFoundException e) {
			System.out.println("File not found");
		} catch (IOException e) {
			System.out.println("appendCity : failed to append");
		}
	}

	public void updateCity(String oldCity, String newCity) {
		FileWriter fw;
		PrintWriter pw;
		try {
			File cityFile = getCitiesFile();
			File tempFile = new File("./temp.sg");
			if(tempFile != null && tempFile.exists()) {
				tempFile.delete();
				tempFile.createNewFile();
			}
			tempFile.createNewFile();
			fw = new FileWriter(tempFile);
			pw = new PrintWriter(fw);
			FileReader fr = new FileReader(cityFile);
			BufferedReader br = new BufferedReader(fr);
			String line = "";
			while((line = br.readLine()) != null) {
				if(line.trim().equals(oldCity)) {
					line = newCity;
				}
				pw.write(line);
				pw.write("\n");
			}
			fw.flush();
			pw.flush();
			fw.close();
			pw.close();
			fr.close();
			br.close();
			cityFile.delete();
			tempFile.renameTo(cityFile);
		} catch (IOException e) {
			System.out.println("updateCity : failed to update");
		}

	}

	public void deleteCity(String city) {
		FileWriter fw;
		PrintWriter pw;
		try {
			File cityFile = getCitiesFile();
			File tempFile = new File("./temp.sg");
			if(tempFile != null && tempFile.exists()) {
				tempFile.delete();
				tempFile.createNewFile();
			}
			tempFile.createNewFile();
			fw = new FileWriter(tempFile);
			pw = new PrintWriter(fw);
			FileReader fr = new FileReader(cityFile);
			BufferedReader br = new BufferedReader(fr);
			String line = "";
			while((line = br.readLine()) != null) {
				if(line.trim().equals(city)) {
					line = null;
				}
				if(line != null) {
					pw.write(line);
					pw.write("\n");
				}
			}
			fw.flush();
			pw.flush();
			fw.close();
			pw.close();
			fr.close();
			br.close();
			cityFile.delete();
			tempFile.renameTo(cityFile);
		} catch (IOException e) {
			System.out.println("updateCity : failed to update");
		}

	}

	@Override
	public void saveCities(List<String> cities) {
		// TODO Auto-generated method stub

	}
}
